package com.eve.springcloud.controller;

import java.net.URI;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.eve.common.util.Jsons;
import com.eve.springcloud.entity.SysDeptEntity;
import com.eve.springcloud.service.SysDeptService;

@RestController
public class SysDeptController {

	@Resource
	private SysDeptService sysDeptService;
	
//	@Resource
	private DiscoveryClient discovery;
	
	@RequestMapping(value = "/dept/save")
	public @ResponseBody Jsons save(@RequestBody SysDeptEntity sysDept) {
		int saveDept = sysDeptService.saveDept(sysDept);
		return Jsons.ok().put("add", saveDept);
	}
	
	@RequestMapping(value = "/dept/find/{deptId}")
	public @ResponseBody Jsons find(@PathVariable("deptId") Long deptId) {
		SysDeptEntity find = sysDeptService.findOne(deptId);
		return Jsons.ok().put("find", find);
	}
	/**
	 * 发现当前服务中的Zookeeper已注册过的服务信息
	 * @return
	 */
	@RequestMapping(value = "/dept/discovery")
	public @ResponseBody Jsons discovery() {
		// 应用名称列表
		List<String> serviceList = discovery.getServices();
		// 具体应用有哪些实例
		/*List<ServiceInstance> instances = discovery.getInstances("CLOUD-PAYMENT-SERVICE");
		for (ServiceInstance serviceInstance : instances) {
			// 服务id
			String serviceId = serviceInstance.getServiceId();
			// 主机地址
			String host = serviceInstance.getHost();
			// 端口
			int port = serviceInstance.getPort();
			// URL
			URI uri = serviceInstance.getUri();
			System.out.println(serviceId);
			System.out.println(host + "," + port);
			System.out.println(uri);
		}*/
		return Jsons.ok().put("data", serviceList);
	}
	
}
